package com.mcxx.common.service.impl;

import cn.hutool.core.util.IdUtil;
import com.mcxx.common.service.InspectionService;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.order.dto.InspectionBatchDTO;
import com.mcxx.modules.miniSalvation.order.dto.InspectionUserDTO;
import com.mcxx.modules.miniSalvation.order.entity.InspectionBatchEntity;
import com.mcxx.modules.miniSalvation.order.service.read.InspectionOrderReadService;
import com.mcxx.modules.miniSalvation.order.service.read.InspectionUserReadService;
import com.mcxx.modules.miniSalvation.order.service.write.InspectionBatchWriteService;
import com.mcxx.modules.miniSalvation.order.service.write.InspectionOrderWriteService;
import com.mcxx.modules.miniSalvation.order.service.write.InspectionUserWriteService;
import com.mcxx.modules.miniSalvation.order.service.write.OrderInspectionWriteService;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.util.Constant;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;


@Service
@TaTransactional
public class InspectionServiceImpl implements InspectionService {

    private final static Logger logger = LoggerFactory.getLogger(InspectionServiceImpl.class);

    @Autowired
    private InspectionBatchWriteService inspectionBatchWriteService;
    @Autowired
    private InspectionUserReadService inspectionUserReadService;
    @Autowired
    private InspectionUserWriteService inspectionUserWriteService;
    @Autowired
    private InspectionOrderReadService inspectionOrderReadService;
    @Autowired
    private InspectionOrderWriteService inspectionOrderWriteService;
    @Autowired
    private OrderInspectionWriteService orderInspectionWriteService;
    @Autowired
    private ExportExcelService exportExcelService;
    @Autowired
    private FileService fileService;
    @Autowired
    private TaskExportWriteService taskExportWriteService;


    @Override
    public void createBatch(InspectionBatchDTO batchDTO, UserAccountVo accountVo) {

        InspectionBatchEntity batchEntity = new InspectionBatchEntity();
        BeanUtils.copyProperties(batchDTO, batchEntity);
        batchEntity.setId(IdUtil.simpleUUID());
        batchEntity.setBatchState(Constant.InspectionBatchState.INIT);
        batchEntity.setBatchResult(Constant.BatchPushState.NO);
        batchEntity.setCreateName(accountVo.getName());
        batchEntity.setCreateTime(new Date());
        batchEntity.setOrgId(accountVo.getOrgId());
        inspectionBatchWriteService.insertIgnoreNull(batchEntity);





        List<InspectionUserDTO> users = batchDTO.getUsers();
        users.forEach(user -> {
            user.setId(IdUtil.simpleUUID());
            user.setBatchId(batchEntity.getId());
            if (Constant.Intermediary.YES.equals(batchDTO.getIntermediary())) {

                user.setPassword("abcd1234");
            }
        });


        inspectionUserWriteService.batchInsert(users);
    }


    @Override
    public void addBatchItem(String batchId, List<String> orderIds) {

        inspectionOrderWriteService.batchInsert(batchId, orderIds);


        updateHouseholds(batchId);
    }


    @Override
    public void deleteBatchItem(String batchId, List<String> items) {

        inspectionOrderWriteService.batchDelete(batchId, items);


        updateHouseholds(batchId);
    }


    @Override
    public void deleteBatch(String id) {

        orderInspectionWriteService.deleteByBatchId(id);

        inspectionOrderWriteService.deleteByBatchId(id);

        inspectionUserWriteService.deleteByBatchId(id);

        InspectionBatchEntity batchEntity = new InspectionBatchEntity();
        batchEntity.setId(id);
        inspectionBatchWriteService.delete(batchEntity);
    }


    @Override
    public ExcelInfo exportInspectors(String batchId,UserAccountVo userAccountVo) {
        List<InspectionUserDTO> users = inspectionUserReadService.listByBatchId(batchId);
        ExcelInfo excelInfo = exportExcelService.exportOrgOpLogs(InspectionUserDTO.class,users,"督查人员信息.xls",
                "督查人员信息","督查人员信息",false,"");
        if(null != excelInfo){
            InputStream sbs = new ByteArrayInputStream(excelInfo.getFile());
            String objectkey = fileService.upload(sbs,excelInfo.getFileName());
            if(StringUtils.isNotBlank(objectkey)){
                TaskExportEntity taskExport = new TaskExportEntity();
                taskExport.setId(Constant.getUUID());
                taskExport.setUserid(userAccountVo.getUserId());
                taskExport.setTaskName(excelInfo.getFileName());
                taskExport.setTaskTitle("数据督查列表");
                taskExport.setParam("xls");
                taskExport.setCreateTime(new Date());
                taskExport.setFileUrl(objectkey);
                taskExport.setState("02");
                int i = taskExportWriteService.insertIgnoreNull(taskExport);
                if(i > 0){
                    excelInfo = null;
                }
            }
        }
        return excelInfo;
    }


    public void updateHouseholds(String batchId) {

        int households = inspectionOrderReadService.countByBatchId(batchId);


        InspectionBatchEntity batchEntity = new InspectionBatchEntity();
        batchEntity.setId(batchId);
        batchEntity.setHouseholder(households);
        batchEntity.setBatchState(households == 0 ? Constant.InspectionBatchState.INIT : Constant.InspectionBatchState.UNCONFIRMED);
        inspectionBatchWriteService.updateIgnoreNull(batchEntity);
    }
}
